#include "contorlcamera.h"
#include<termios.h>
#include<unistd.h>
#include<QDebug>
ContorlCamera::ContorlCamera(int fd)
{
    this->fd = fd;
}

void ContorlCamera::initChar()
{
    ::memset(FirstOpenCmd,'\0',sizeof(FirstOpenCmd));
    ::memset(FirstCloseCmd,'\0',sizeof(FirstCloseCmd));
    ::memset(SecondOpenCmd,'\0',sizeof(SecondOpenCmd));
    ::memset(SecondCloseCmd,'\0',sizeof(SecondCloseCmd));
}

bool ContorlCamera::FirstOpenCamera()
{
    qDebug()<<"send to picture!";
    initChar();
    FirstOpenCmd[0]=0xFE;
    FirstOpenCmd[1]=0x10;
    FirstOpenCmd[2]=0x00;
    FirstOpenCmd[3]=0x03;
    FirstOpenCmd[4]=0x00;
    FirstOpenCmd[5]=0x02;
    FirstOpenCmd[6]=0x04;
    FirstOpenCmd[7]=0x00;
    FirstOpenCmd[8]=0x04;
    FirstOpenCmd[9]=0x00;
    FirstOpenCmd[10]=0x05;
    FirstOpenCmd[11]=0x01;
    FirstOpenCmd[12]=0x6F;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,FirstOpenCmd,13);
    qDebug()<<"write len="<<len;
    if(len==13)
        return true;
    else
        return false;

}

bool ContorlCamera::FirstCloseCamera()
{
    initChar();
    FirstCloseCmd[0]=0xFE;
    FirstCloseCmd[1]=0x05;
    FirstCloseCmd[2]=0x00;
    FirstCloseCmd[3]=0x00;
    FirstCloseCmd[4]=0x00;
    FirstCloseCmd[5]=0x00;
    FirstCloseCmd[6]=0xD9;
    FirstCloseCmd[7]=0xC5;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,FirstCloseCmd,sizeof(FirstCloseCmd));
    if(len==8)
        return true;
    else
        return false;
}

bool ContorlCamera::SecondOpenCamera()
{
    initChar();
    SecondOpenCmd[0]=0xFE;
    SecondOpenCmd[1]=0x10;
    SecondOpenCmd[2]=0x00;
    SecondOpenCmd[3]=0x08;
    SecondOpenCmd[4]=0x00;
    SecondOpenCmd[5]=0x02;
    SecondOpenCmd[6]=0x04;
    SecondOpenCmd[7]=0x00;
    SecondOpenCmd[8]=0x04;
    SecondOpenCmd[9]=0x00;
    SecondOpenCmd[10]=0x05;
    SecondOpenCmd[11]=0x40;
    SecondOpenCmd[12]=0xDC;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,SecondOpenCmd,13);
    if(len==13)
        return true;
    else
        return false;
}

bool ContorlCamera::SecondCloseCamera()
{
    initChar();
    SecondCloseCmd[0]=0xFE;
    SecondCloseCmd[1]=0x05;
    SecondCloseCmd[2]=0x00;
    SecondCloseCmd[3]=0x01;
    SecondCloseCmd[4]=0x00;
    SecondCloseCmd[5]=0x00;
    SecondCloseCmd[6]=0x88;
    SecondCloseCmd[7]=0x05;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,SecondCloseCmd,sizeof(SecondCloseCmd));
    if(len==8)
        return true;
    else
        return false;
}


void ContorlCamera::run()
{
}
